﻿// -----------------------------------------------------------------------
// <copyright file="EstimateValue.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Pattern_package.Create_Correct_Pattern
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public  class EstimateValue
    {
        public void EstimateValueInNormal(ref NormalDistrubution[] normalInfo,Sample_info.InfoOfClass[] data)
        {
            normalInfo = new NormalDistrubution[data.Length];
            for (int i = 0; i < data.Length; i++)
            {
                normalInfo[i].mean = 0;
                for (int j = 0; j < data[i].data.Count; j++)
                {
                    normalInfo[i].mean += data[i].data[j];
                }
                normalInfo[i].mean = normalInfo[i].mean / data[i].data.Count;
            }
            for (int i = 0; i < data.Length; i++)
            {
                normalInfo[i].sigma = 0;
                for (int j = 0; j < data[i].data.Count; j++)
                {
                    normalInfo[i].sigma += Math.Pow(data[i].data[j] - normalInfo[i].mean, 2);
                }
                normalInfo[i].sigma = Math.Sqrt(normalInfo[i].sigma / data[i].data.Count);
            }
        }

    
        public void CalprobOfClassGivenXNormal(ref double []probOfClassGivenX ,NormalDistrubution[] normalInfo, Sample_info.InfoOfClass[] data,float valueX)
        {
            double[] probOfXgivenClass = new double[data.Length];
            probOfClassGivenX = new double[data.Length];
            for (int i = 0; i < data.Length; i++)
            {
                probOfXgivenClass[i] = (1 / Math.Sqrt(2 * Math.PI * normalInfo[i].sigma)) * Math.Exp(-Math.Pow((valueX - normalInfo[i].mean), 2) / 2 * Math.Pow(normalInfo[i].sigma, 2));
            }
            double probabiltyOfX = 0;
            for (int i = 0; i < data.Length; i++)
            {
                probabiltyOfX += probOfXgivenClass[i] * data[i].prob;
            }
            for (int i = 0; i < data.Length; i++)
            {
                probOfClassGivenX[i] = (probOfXgivenClass[i] * data[i].prob) / probabiltyOfX;
            }
        }
    }
}